<template>
  <div class="container">
    <div class="top">
      <el-select
        v-model="search.supplier_code"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择供应商"
        :remote-method="supplierRemoteMethod"
        :loading="supplierCenter.loading"
      >
        <el-option
          v-for="item in supplierCenter.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!-- <el-select
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        v-model="search.rdc_code"
        placeholder="请搜索选择配送中心"
        :remote-method="selectListsRemoteMethod"
        :loading="selectLists.loading"
      >
        <el-option
          v-for="item in selectLists.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select> -->
      <el-select
        style="margin-bottom: 5px"
        v-model="search.org_code"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择门店"
        :remote-method="organizationMethod"
        :loading="insides.loading"
      >
        <el-option
          v-for="item in insides.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        v-model="search.goods_code"
        placeholder="请搜索选择商品"
        :remote-method="insideRemoteMethod"
        :loading="inside.loading"
      >
        <el-option
          v-for="item in inside.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        type="danger"
        icon="el-icon-delete"
        class="handleSearch"
        @click="empty()"
      >清空</el-button>
      <el-button
        type="primary"
        class="handleSearch"
        icon="el-icon-upload"
        @click="download()"
        >模板下载</el-button
      >

      <el-upload
        class="upload-demo"
        style="display: inline; margin-left: 10px"
        action="#"
        :show-file-list="false"
        :http-request="handleAvatarSuccess"
      >
        <el-button
          type="primary"
          class="handleSearch"
          icon="el-icon-circle-plus"
          >导入</el-button
        >
      </el-upload>
      <el-button type="info" icon="el-icon-tickets" @click="derive()"
        >导出</el-button
      >
      <el-button
        icon="el-icon-circle-plus-outline"
        type="primary"
        @click="supermarketBind()"
        >绑定</el-button
      >
      <el-button icon="el-icon-remove-outline" type="primary" @click="unbinds()"
        >解绑</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      empty-text="暂无数据"
    >
      <el-table-column type="selection" align="center"></el-table-column>
      <el-table-column
        prop="supplier_code"
        label="供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_code"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="rdc_code"
        label="配送中心编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rdc_name"
        label="配送中心"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="org_code"
        label="门店编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="org_name"
        label="门店"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_code"
        label="分类编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="分类"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-remove-outline"
            type="primary"
            @click="unbind(scope)"
            >解绑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- dialog -->
    <suppliersSupermarketDialog
      :dialog="bindDialog"
      @upData="getData"
    ></suppliersSupermarketDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import {
  bindingList,
  unbind,
  selectErpGoodsAllData
} from "@/api/suppliersSupermarket/suppliersSupermarketList";
import { selectSupplierData } from "@/api/supplier/supplierList"; //供应商
import { selectRdcData } from "@/api/select/select";
import { getOpenOrgData } from "@/api/middleorder/middleorderlist";
import { selectErpOrderGoodsData } from "@/api/superOrder/superOrderList";
import SuppliersSupermarketDialog from "@/components/suppliersSupermarketDialog";
import { getToken } from "@/utils/auth";
import { Loading } from "element-ui";
export default {
  components: {
    pagination: Pagination,
    suppliersSupermarketDialog: SuppliersSupermarketDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      search: {
        goods_code: "",
        org_code: "",
        supplier_code: ""
        // rdc_code: ""
      },
      supplierCenter: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      selectLists: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      inside: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      insides: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      bindDialog: {
        dialogVisible: false,
        id: ""
      },
      multipleSelection: []
    };
  },

  created() {
    //页面数据
    this.getData();
    this.getSupplier();
    this.getselsetData();
    this.organizationList();
    this.getGoodsData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        goods_code: this.search.goods_code,
        org_code: this.search.org_code,
        supplier_code: this.search.supplier_code
        // rdc_code: this.search.rdc_code
      };
      bindingList(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //下载模板
    download() {
      this.$confirm("是否下载模板?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Binding/downLoadTmp?token=" +
            getToken();
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //导入
    handleAvatarSuccess(param) {
      this.noList = false;
      this.tags = [];
      let loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)"
      });

      let data = new window.FormData();
      data.append("token", getToken());
      data.append("upfile", param.file);
      this.$axios.post("/smkapi/Binding/importBind", data).then(res => {
        loading.close();
        if (res.data.code == 0) {
          this.getData();
        } else {
          this.$message({
            type: "info",
            message: res.data.message || res.data.msg
          });
        }
      });
    },
    // 导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Binding/exportBindXlsx?token=" +
            getToken() +
            "&supplier_code=" +
            this.search.supplier_code +
            // "&rdc_code=" +
            // this.search.rdc_code +
            "&org_code=" +
            this.search.org_code +
            "&goods_code=" +
            this.search.goods_code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //绑定
    supermarketBind() {
      this.bindDialog.dialogVisible = true;
    },
    // 解绑
    unbind(scope) {
      let data = {
        ids: scope.row.id
      };
      this.$confirm("是否解绑?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          unbind(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //多个解绑
    unbinds() {
      if (this.multipleSelection.length > 0) {
        let ids = [];
        this.multipleSelection.forEach(res => {
          ids.push(res.id);
        });
        let data = {
          ids: ids.join(",")
        };
        this.$confirm("是否解绑?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            unbind(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "请勾选内容"
        });
      }
    },
    // 供应商数据
    getSupplier(name = "") {
      let data = {
        page: 1,
        limit: 100,
        name
      };
      //获取供应商列表
      selectSupplierData(data).then(res => {
        this.supplierCenter.option = res.data.paginate.data.map(item => {
          return {
            value: `${item.supplier_code}`,
            label: `${item.supplier_name}`
          };
        });
      });
    },

    // 搜索供应商数据
    supplierRemoteMethod(query) {
      if (query !== "") {
        this.getSupplier(query);
      } else {
        this.supplierCenter.option = [];
      }
    },
    //配送中心数据
    getselsetData(name = "") {
      let data = {
        page: 1,
        limit: 500,
        code_or_name: name
      };

      selectRdcData(data).then(res => {
        if (res.data.paginate.data) {
          this.selectLists.option = res.data.paginate.data.map(item => {
            return {
              value: `${item.d_code}`,
              label: `${item.d_name}`
            };
          });
        }
      });
    },
    // 搜索配送中心数据
    selectListsRemoteMethod(query) {
      if (query !== "") {
        this.getselsetData(query);
      } else {
        this.selectLists.option = [];
      }
    },
    // 门店数据
    organizationList(name = "") {
      let data = {
        page: 1,
        limit: 500,
        key: name
      };
      getOpenOrgData(data).then(res => {
        if (res.data) {
          this.insides.option = res.data.map(item => {
            return {
              value: `${item.erp_org_code}`,
              label: `${item.name}`
            };
          });
        }
      });
    },
      //清空
      empty() {
      this.search = {
        goods_code: "",
        org_code: "",
        supplier_code: ""
      };
     
    },
    // 搜索门店数据
    organizationMethod(query) {
      if (query !== "") {
        this.organizationList(query);
      } else {
        this.insides.option = [];
      }
    },
    // 商品数据
    getGoodsData(name) {
      let data = {
        page: 1,
        limit: 500,
        goods_code: name
      };
      selectErpGoodsAllData(data).then(res => {
        this.goodsList = res.data.paginate.data;
        if (res.data.paginate.data) {
          this.inside.option = res.data.paginate.data.map(item => {
            return { value: `${item.Code}`, label: `${item.Name}` };
          });
        }
      });
    },
    // 搜索商品数据
    insideRemoteMethod(query) {
      if (query !== "") {
        this.getGoodsData(query);
      } else {
        this.inside.options = [];
      }
    },
    //删除
    handleDelete(scope) {
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            id: scope.row.id
          };
          getdelRegion(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      console.log(val, 99999);
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 10px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
